
(function(){
    //地图区模块造器
    function MapMode(){}//地图模块
    MapMode.prototype={
        //地图的初始化
        mapInit:function(opts){
            this.drawLineMap=opts.drawLineMap;
            this.renderMarker=opts.renderMarker;//图标  开始    结束
            this.renderPolyline=opts.renderPolyline;
            this.startPoint=opts.startPoint;///起点的图标
            this.endPoint=opts.endPoint;//终点的图标
            this.drawMap=opts.drawRouteMap;//绘图 路线绑定
            this.acceptParam=opts.sendData;//账号
            this.trackData();//Vue 轨迹数据
            this.mapRouteAjax(this,opts.drawLineMap);//轨迹绑定 默认不锁路
            this.slideFullScreen(opts.slideMode);//滑动全屏
            this.showTrack(opts.routeShow);
        },
        trackData:function(){
            var dataCollection=this.acceptParam;//参数的集合
            new Vue({
                el:"#tab-cont",
                data:{
                    distance:dataCollection.distance,//Vue模板  总路程
                    timesStart:baseAll.dateToString(Number(dataCollection.beginTime)),//Vue模板  设置开始时间
                    timesEnd:baseAll.dateToString(Number(dataCollection.endTime)),//Vue模板  设置结束时间
                    gapTime:Number(dataCollection.endTime)-Number(dataCollection.beginTime),//Vue模板  计算出时间差
                    beginPos:dataCollection.beginPos,//Vue模板  开始位置
                    endPos:dataCollection.endPos  //Vue模板  结束位置
                },
                methods:{
                    //耗时计算
                    trackTime:function(seconds){
                        seconds=Math.floor(seconds/1000);
                        //当时间小于1小时,则只显示分钟数
                        if(seconds<3600){
                            return Math.floor(seconds/60)+"分钟";
                        }else{
                            //当时间大于1小时,则分钟数和小时
                            var hour=Math.floor(seconds/3600);
                            var minute=Math.floor((seconds-hour*3600)/60);
                            return hour+"时"+minute+"分钟";
                        }
                    },
                    //平均路程计算
                    averageSpeed:function(seconds){
                        seconds=Math.floor(seconds/1000);
                        var totalDistance=dataCollection.distance;//获取总路程[公里]
                        var time=Math.floor(seconds/3600)+(seconds%3600)/3600;//计算耗时[小时]
                        return   (totalDistance/time).toFixed(1);//将匀速抛出到Vue模板
                    }
                }
            })
        },
        //请求经纬度位置
        mapRouteAjax:function(self,handle){
            var dataCollection=this.acceptParam;//参数的集合
            baseAll.getIpByAccount(dataCollection.account,function(contextPath){
                baseAll.addLoading();
                $.ajax({
                    type:"post",
                    url:contextPath+"/s/gprs/path/"+dataCollection.imeiId,
                    async:true,
                    crossDomain:true,
                    dataType:"json",
                    timeout:baseAll.timeout,
                    data:{
                        mapType:"baidu",
                        token:dataCollection.token,
                        timeBegin:baseAll.dateToString(dataCollection.beginTime-0),
                        timeEnd:baseAll.dateToString(dataCollection.endTime-0)
                    },
                    success:function(data){
                        if(data.opStatus==0){
                            handle&&handle(self,data);//回调抛出到全局
                            baseAll.closeLoading();
                        }else{
                            oPublic.pubilcModal("错误提醒:"+data.msgDesc);
                            baseAll.closeLoading();
                        }
                    },
                    error:function(){
                        oPublic.pubilcModal("网络连接失败");
                        baseAll.closeLoading();
                    }
                })
            });
        },
        //滑动全屏显示效果----------
        slideFullScreen:function(obj){
            var upDis=obj.up;//上滑动距离
            var bottomDis=obj.bottom;//下滑动距离
            $(obj.ele).on("touchstart",function(){
                var eleClassName=$(this).attr("class");
                if(eleClassName.indexOf("take_back")===-1){
                    $(this).addClass("take_back");
                    $(".top_fixed").finish().animate({"top":-upDis+"rem","opacity":0},"slow","swing");
                    $(".bot_Box").finish().animate({"bottom":-bottomDis+"rem","opacity":0},"slow","swing");
                    $(".map_manger").finish().animate({"top":1+"rem"},"slow","swing");
                }else{
                    $(this).removeClass("take_back");
                    $(".top_fixed").finish().animate({"top":0,"opacity":1},"slow","swing");
                    $(".bot_Box").finish().animate({"bottom":0,"opacity":1},"slow","swing");
                    $(".map_manger").finish().animate({"top":3.1+"rem"},"slow","swing");
                }
            })
        },
        //显示路线切换
        showTrack:function(opt){
            var $this=this;
            $(opt).find("a").on("touchstart",function(){
                 var index=$(this).index();
                 if(index==0){
                     $this.mapRouteAjax($this,$this.drawLineMap);//地图绘制路径模板  轨迹连线
                 }else{
                     $this.mapRouteAjax($this,$this.drawMap);//地图绘制路径模板    轨迹连线【绑路】
                 }
            })

        }
    };
    window.MapMode=new MapMode();
})();




//参数配置------------------------------------------------------
/*
*    MapRouteTrack  ---function  该方法直接接入不同的设备
*    Android  IOS
* */
var MapRouteTrack=function(sendData){
    /*
        * @param   senData             接受的参数详情  Object
        * @param   senData=>token      token
        * @param   senData=>account    用户账号
        * @param   senData=>imeiId     imeiId查询轨迹的编号
        * @param   senData=>distance    行驶长度
        * @param   senData=>beginPos    开始位置
        * @param   senData=>endPos      结束位置
        * @param   senData=>beginTime   开始时间
        * @param   senData=>endTime     结束时间
    * */
    MapMode.mapInit({
        startPoint:"../../common/image/start_point.png",//起点的图标
        endPoint:"../../common/image/end_point.png",//终点的图标
        slideMode:{
            ele:"#show_full_screen",//全屏地图显示
            up:4,//上滑动距离
            bottom:5//下滑动距离
        },
        sendData:sendData,
        routeShow:".track_road",//轨迹路线渲染
        renderMarker : function (lng ,lat,icon) {
            var point = new BMap.Point(lng,lat);
            var marker = new BMap.Marker(point,{icon:icon});
            return marker;
        },
        renderPolyline :function (lng1,lat1,lng2,lat2) {
            var arr = [];
            arr.push(new BMap.Point(lng1,lat1));
            arr.push(new BMap.Point(lng2,lat2));
            var polyline = new BMap.Polyline(arr,{strokeColor:"#F7504C", strokeWeight:2, strokeOpacity:1});
            return polyline;
        },
        drawRouteMap:function(self,data){
            var data = data.data;
            var map = new BMap.Map("allmap");
            map.centerAndZoom(new BMap.Point(data[0].lng,data[0].lat),25);
            map.setZoom(25);
            map.enableScrollWheelZoom(true);
            var route = new BMap.DrivingRoute(map);
            var dataLength = data.length ;
            var beginP = new BMap.Point(data[0].lng,data[0].lat);
            var endP = new BMap.Point(data[dataLength -1].lng,data[dataLength -1].lat);
            var pathWays = [];//存储设置途径的坐标点
            var mod=parseInt(dataLength/5);
            //设置途径的坐标点---------------
            //alert(dataLength);
            if(dataLength>2){
                for(var k=0;k<mod.length;k++){
                    if(k%10==0){
                        pathWays.push(new BMap.Point(data[k].lng,data[k].lat));
                    }
                }
            }

            //路线绑定功能--------------
            route.setSearchCompleteCallback(function (results) {
                map.clearOverlays();
                //重置-----------------------
                var beginIcn = new BMap.Icon(self.startPoint, new BMap.Size(35,39));
                var endIcn = new BMap.Icon(self.endPoint, new BMap.Size(35,39));
                var lastPoint = null;
                var routeResult = results.getPlan(0);
                var routeNum = routeResult.getNumRoutes();
                var pts = route.getResults().getPlan(0).getRoute(0).getPath();
                console.log(JSON.stringify(pts));
                for(var i = 0 ; i< routeNum ; i++){
                    var routeResultPoint = results.getPlan(0).getRoute(i).getPath();
                    for(var j= 0; j < routeResultPoint.length ;j ++) {
                        var pointInfo = routeResultPoint[j];
                        var icon = {};
                        if(j === 0 && i ===0){
                            icon = beginIcn;
                        }else if(j === routeResultPoint.length -1 && i === routeNum -1){
                            icon = endIcn;
                        }else{
                            icon = null;
                        }
                        var marker = self.renderMarker(pointInfo.lng ,pointInfo.lat,icon);
                        if(icon){
                            map.addOverlay(marker);
                        }
                        if(lastPoint != null){
                            var polyline = self.renderPolyline(pointInfo.lng,pointInfo.lat,lastPoint.lng,lastPoint.lat);
                            map.addOverlay(polyline);
                        }
                        lastPoint = pointInfo;
                    }
                }
            });
            route.search(beginP,endP,{waypoints:pathWays});
        },
        //路线的直接连接**[不经过绑路设置]--------------
        drawLineMap:function(self,data){
            var dataJSON = data.data;
            var map = new BMap.Map("allmap");
            map.centerAndZoom(new BMap.Point(dataJSON[0].lng,dataJSON[0].lat),28);
            map.setZoom(25);
            map.enableScrollWheelZoom(true);
            //设置起点图标---
            var beginIcn = new BMap.Icon(self.startPoint, new BMap.Size(35,39));
            var markerBegin = self.renderMarker(dataJSON[0].lng ,dataJSON[0].lat,beginIcn);
            if(beginIcn){
                map.addOverlay(markerBegin);
            }
            //设置终点的图标---
            var endIcn = new BMap.Icon(self.endPoint, new BMap.Size(35,39));
            var lastPonit=dataJSON.length-1;//终点坐标
            var markerEnd = self.renderMarker(dataJSON[lastPonit].lng ,dataJSON[lastPonit].lat,endIcn);
            if(endIcn){
                map.addOverlay(markerEnd);
            }
            //储存经纬度坐标点
            var arrPoint=[];
            //通过循环将所有的经纬坐标点储存在数组中
            for(var k=0;k<dataJSON.length;k++){
                arrPoint.push(new BMap.Point(dataJSON[k].lng,dataJSON[k].lat));
            }
            //将所有坐标点绘制成线条[未绑路的状态]------
            var polyline = new BMap.Polyline(arrPoint,{strokeColor:"#F7504C", strokeWeight:2, strokeOpacity:1});   //创建折线
            map.addOverlay(polyline);
        }
    });
}